package com.mall.config;

import cn.dev33.satoken.interceptor.SaInterceptor;
import com.mall.common.JacksonObjectMapper;
import com.mall.common.LimitInterceptor;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

/**
 * @Package: com.mall.config
 * @author: xiao-chen
 * @Create: 2023/4/13-16:03
 * @Version: v 1.0
 * @Description:
 */
@Configuration
public class GlobalConfig implements WebMvcConfigurer {
    @Resource
    JacksonObjectMapper jacksonObjectMapper;
    @Resource
    LimitInterceptor limitInterceptor;


    /**
     * @Description: 将自定义的消息转换器注入到容器中，让容器优先使用自定义的转换器
     * @param: [java.util.List<org.springframework.http.converter.HttpMessageConverter < ?>>]
     * @return: void
     * @author: xiao-chen
     * @date: 2023/4/2 13:56
     */

    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        converter.setObjectMapper(jacksonObjectMapper);
        converters.add(0, converter);
    }

    /**
     * 将sa-token中的拦截器和自定义的限流拦截器注册到容器中
     *
     * @param registry
     * @return void
     * @author cxx
     * @date 2023/5/12 9:20
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new SaInterceptor()).addPathPatterns("/**").order(2);
        registry.addInterceptor(limitInterceptor).addPathPatterns("/**")
                .excludePathPatterns("/admin/login", "/user/login", "/registry", "/captcha", "/getSms", "/afterPay", "/notify").order(1);
    }
}
